'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Net
Imports System.IO
Imports ua

Public Class frmAdd
    Inherits System.Windows.Forms.Form

    Public EditFlag As Boolean
    Public FormParent As Form
    Public FormType As Integer
    Private AppDataPath As String = GetAppDataPath()

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        'make sure entries are valid
        If FormType = 1 Then
            If txtImageURL.Text = "" Or txtImageURL.Text = "http://" Then
                MessageBox.Show("URL Required", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                TabControl1.SelectedIndex = 0
                txtImageURL.Focus()
                Exit Sub
            End If

            If txtLabel.Text = "" Then
                MessageBox.Show("LABEL Required", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                TabControl1.SelectedIndex = 0
                txtLabel.Focus()
                Exit Sub
            End If

            If txtOpacity.Text = "" Then
                MessageBox.Show("FORM OPACITY Required", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                TabControl1.SelectedIndex = 0
                txtOpacity.Focus()
                Exit Sub
            End If

            If Val(txtOpacity.Text) < 0 Or Val(txtOpacity.Text) > 1 Then
                MessageBox.Show("Invalid value for OPACITY. Must be between 0 and 1", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                TabControl1.SelectedIndex = 0
                txtOpacity.Focus()
                Exit Sub
            End If
        End If

        If txtFormWidth.Text = "" Then
            MessageBox.Show("FORM WIDTH Required", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            TabControl1.SelectedIndex = 1
            txtFormWidth.Focus()
            Exit Sub
        End If

        If txtFormHeight.Text = "" Then
            MessageBox.Show("FORM HEIGHT Required", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            TabControl1.SelectedIndex = 1
            txtFormHeight.Focus()
            Exit Sub
        End If

        If txtX.Text = "" Then
            MessageBox.Show("X FORM POSITION Required", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            TabControl1.SelectedIndex = 1
            txtX.Focus()
            Exit Sub
        End If

        If txtY.Text = "" Then
            MessageBox.Show("Y FORM POSITION Required", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            TabControl1.SelectedIndex = 1
            txtY.Focus()
            Exit Sub
        End If

        'set values
        If EditFlag = False Then
            Dim NewFormData As WallpaperFormData

            If FormType = 0 Then
                NewFormData = New WallpaperFormData(0)
            Else
                NewFormData = New WallpaperFormData(1)
                NewFormData.ShowLabel = chkShowLabel.Checked
                NewFormData.Label = txtLabel.Text
                NewFormData.URL = txtImageURL.Text
                NewFormData.StretchImage = chkImageStretch.Checked
                NewFormData.MaintainAspectRatio = chkImageAspectRatio.Checked
                NewFormData.Zoom = chkImageZoom.Checked
                NewFormData.ZoomX = Val(txtZoomX.Text).ToString
                NewFormData.ZoomY = Val(txtZoomY.Text).ToString
                NewFormData.ZoomWidth = Val(txtZoomWidth.Text).ToString
                NewFormData.ZoomHeight = Val(txtZoomHeight.Text).ToString
                NewFormData.ShowBackground = chkShowBackground.Checked
                NewFormData.BackColour = Format(pbxBackgroundColour.BackColor.ToArgb)
                NewFormData.ForeColour = Format(pbxForegroundColour.BackColor.ToArgb)
                NewFormData.Opacity = Val(txtOpacity.Text).ToString
            End If

            NewFormData.Width = Val(txtFormWidth.Text).ToString
            NewFormData.Height = Val(txtFormHeight.Text).ToString
            NewFormData.X = Val(txtX.Text).ToString
            NewFormData.Y = Val(txtY.Text).ToString

            'add class to list
            CType(FormParent, frmConfiguration).lstForm.Items.Add(NewFormData)
            CType(FormParent, frmConfiguration).btnAddISPForm.Enabled = False
        Else
            'edit data
            Dim NewFormData As WallpaperFormData = CType(CType(FormParent, frmConfiguration).lstForm.Items(CType(FormParent, frmConfiguration).lstForm.SelectedIndex), WallpaperFormData)

            If FormType = 0 Then
                NewFormData.FormType = 0
            Else
                NewFormData.ShowLabel = chkShowLabel.Checked
                NewFormData.Label = txtLabel.Text
                NewFormData.URL = txtImageURL.Text
                NewFormData.StretchImage = chkImageStretch.Checked
                NewFormData.MaintainAspectRatio = chkImageAspectRatio.Checked
                NewFormData.Zoom = chkImageZoom.Checked
                NewFormData.ZoomX = Val(txtZoomX.Text).ToString
                NewFormData.ZoomY = Val(txtZoomY.Text).ToString
                NewFormData.ZoomWidth = Val(txtZoomWidth.Text).ToString
                NewFormData.ZoomHeight = Val(txtZoomHeight.Text).ToString
                NewFormData.ShowBackground = chkShowBackground.Checked
                NewFormData.BackColour = Format(pbxBackgroundColour.BackColor.ToArgb)
                NewFormData.ForeColour = Format(pbxForegroundColour.BackColor.ToArgb)
                NewFormData.Opacity = Val(txtOpacity.Text).ToString
            End If

            NewFormData.Width = Val(txtFormWidth.Text).ToString
            NewFormData.Height = Val(txtFormHeight.Text).ToString
            NewFormData.X = Val(txtX.Text).ToString
            NewFormData.Y = Val(txtY.Text).ToString
            CType(FormParent, frmConfiguration).lstForm.Items(CType(FormParent, frmConfiguration).lstForm.SelectedIndex) = NewFormData
        End If

        CType(FormParent, frmConfiguration).lstForm.Refresh()
        CType(FormParent, frmConfiguration).ChangeMade = True
        Me.Close()
    End Sub

    Private Sub cboOpacity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboOpacity.SelectedIndexChanged
        txtOpacity.Text = (Val(cboOpacity.Text) / 100).ToString
    End Sub

    Private Sub btnForegroundColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForegroundColor.Click
        ColorDialog1.Color = pbxForegroundColour.BackColor
        Dim RetVal As DialogResult = ColorDialog1.ShowDialog()
        If RetVal = Windows.Forms.DialogResult.OK Then
            pbxForegroundColour.BackColor = ColorDialog1.Color
        End If
    End Sub

    Private Sub btnBackgroundColour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackgroundColour.Click
        ColorDialog1.Color = pbxBackgroundColour.BackColor
        Dim RetVal As DialogResult = ColorDialog1.ShowDialog()
        If RetVal = Windows.Forms.DialogResult.OK Then
            pbxBackgroundColour.BackColor = ColorDialog1.Color
        End If
    End Sub

    Private Sub cboPosition_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPosition.SelectedIndexChanged
        Dim ImageX As Integer = CInt(txtFormWidth.Text)
        Dim ImageY As Integer = CInt(txtFormHeight.Text)
        If (FormType = 1 And chkShowLabel.Checked = True) Then
            ImageY += 13 + 2
        End If

        If cboPosition.SelectedIndex = 0 Then
            txtX.Text = "0"
            txtY.Text = "0"
        ElseIf cboPosition.SelectedIndex = 1 Then
            txtX.Text = ((SystemInformation.WorkingArea.Width / 2) - (ImageX / 2)).ToString
            txtY.Text = "0"
        ElseIf cboPosition.SelectedIndex = 2 Then
            txtX.Text = (SystemInformation.WorkingArea.Width - ImageX).ToString
            txtY.Text = "0"
        ElseIf cboPosition.SelectedIndex = 3 Then
            txtX.Text = "0"
            txtY.Text = ((SystemInformation.WorkingArea.Height / 2) - (ImageY / 2)).ToString
        ElseIf cboPosition.SelectedIndex = 4 Then
            txtX.Text = ((SystemInformation.WorkingArea.Width / 2) - (ImageX / 2)).ToString
            txtY.Text = ((SystemInformation.WorkingArea.Height / 2) - (ImageY / 2)).ToString
        ElseIf cboPosition.SelectedIndex = 5 Then
            txtX.Text = (SystemInformation.WorkingArea.Width - ImageX).ToString
            txtY.Text = ((SystemInformation.WorkingArea.Height / 2) - (ImageY / 2)).ToString
        ElseIf cboPosition.SelectedIndex = 6 Then
            txtX.Text = "0"
            txtY.Text = (SystemInformation.WorkingArea.Height - ImageY).ToString
        ElseIf cboPosition.SelectedIndex = 7 Then
            txtX.Text = ((SystemInformation.WorkingArea.Width / 2) - (ImageX / 2)).ToString
            txtY.Text = (SystemInformation.WorkingArea.Height - ImageY).ToString
        ElseIf cboPosition.SelectedIndex = 8 Then
            txtX.Text = (SystemInformation.WorkingArea.Width - ImageX).ToString
            txtY.Text = (SystemInformation.WorkingArea.Height - ImageY).ToString
        End If
    End Sub

    Private Sub txtImageURL_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtImageURL.LostFocus
        If txtImageURL.Text.StartsWith("http://") = False Then
            txtImageURL.Text = "http://" & txtImageURL.Text
        End If
    End Sub

    Private Sub chkImageZoom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkImageZoom.CheckedChanged
        If chkImageZoom.Checked = True Then
            EnableControlChildren(grbZoom)
        Else
            DisableControlChildren(grbZoom)
        End If
    End Sub

      Private Sub chkShowBackground_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowBackground.CheckedChanged
        If chkShowBackground.Checked = False Then
            'enable options
            Label14.Enabled = False
            pbxBackgroundColour.Enabled = False
            btnBackgroundColour.Enabled = False
        Else
            'disable options
            Label14.Enabled = True
            pbxBackgroundColour.Enabled = True
            btnBackgroundColour.Enabled = True
        End If
    End Sub

    Private Sub frmAdd_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblWidth.Text = "Width: " & SystemInformation.WorkingArea.Width
        lblHeight.Text = "Height: " & SystemInformation.WorkingArea.Height

        If FormType = 0 Then
            TabControl1.TabPages.Clear()
            TabControl1.TabPages.Add(tpDetails)
            txtFormWidth.Enabled = False
            txtFormHeight.Enabled = False
        Else
            TabControl1.TabPages.Clear()
            TabControl1.TabPages.Add(tpWebImage)
            TabControl1.TabPages.Add(tpDetails)
            grbFormSize.Enabled = True
            txtFormWidth.Enabled = True
            txtFormHeight.Enabled = True
        End If
    End Sub
End Class
